<?php
/*
 * Created on Aug 30, 2012
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
include_once("../configure/configure.php");		//--> global var
include_once("../admin/configure/admin.config.inc.php");		//--> global var

include_once("../admin/lib/apis/GoogleAffiliateApi.php");
include_once("../lib/constant/GlobalConstant.php");

include_once("../lib/impl/FeedImpl.php");
include_once("impl/LogImpl.php");

include_once("../admin/lib/utils/StringUtils.php");

set_time_limit(7200);

$logImpl= new LogImpl(DB_TAG_COUPON);

$ga 	= new GoogleAffiliateApi();

$startTime = $logImpl->getLastRequestTime(GlobalConstant::SOURCE_ID_GOOGLE);

$vo = new LogRequestFeed();
$vo->setStatus('1');
$vo->setSourceId(GlobalConstant::SOURCE_ID_GOOGLE);
$vo->setStartTime($startTime);
$logImpl->add($vo);

$ga->init();

echo "start time: " . $startTime;
echo "\n";

$links = $ga->getLinks(100, 100, $startTime, "", 'approved', "", '');


$feedImpl	= new FeedImpl(DB_TAG_COUPON);

echo "link count: " . count($links);
echo "\n";

$newCouponCount 	= 0;
$newMerchantCount	= 0;

foreach($links as $link){
//	echo "-----------------------------------------------";
//	echo "\n";
	
	if(StringUtils::count_words($link->getName()) < 2)// if words than less 2, ignore this coupon
		continue;
	
	$pos = strripos($link->getName(), "logo");
	if($pos > -1){
		continue;
	}
	
	$couponId = $feedImpl->addCoupon($link);
	if($couponId > 0)
		$newCouponCount++;
		
	if($feedImpl->isAddNewMerchant())
		$newMerchantCount++;
		
//	echo "id: " . $link->getId();
//	echo "\n";
//	
//	echo "name: " . $link->getName();
//	echo "\n";
//	
//	echo "getStartDate: " . $link->getStartDate();
//	echo "\n";
//	
//	echo "getEndDate: " . $link->getEndDate();
//	echo "\n";
//	
//	echo "getImpressionTrackingUrl: " . $link->getImpressionTrackingUrl();
//	echo "\n";
//	
//	echo "getClickTrackingUrl: " . $link->getClickTrackingUrl();
//	echo "\n";
//	
//	echo "getLinkType: " . $link->getLinkType();
//	echo "\n";
//	
//	echo "getDescription: " . $link->getDescription();
//	echo "\n";
//	
//	echo "getPromotionType: " . $link->getPromotionType();
//	echo "\n";
//	
//	echo "getImageAltText: " . $link->getImageAltText();
//	echo "\n";
//	
//	echo "getIsActive: " . $link->getIsActive();
//	echo "\n";
}

$vo->setStatus('2');
$logImpl->setEndTime($vo);

echo "new coupon count: " . $newCouponCount;
echo "\n";

echo "new merchant count: " . $newMerchantCount;
echo "\n";





?>
